package com.ruoyi.common.utils.lrmx;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class LrmxUtils {

    /**
     * Json to xml string.
     *
     * @param json the json
     * @return the string
     */
    public static String jsonToXml(String json){
        try {
            StringBuffer buffer = new StringBuffer();
            buffer.append("<?xml version=\"1.0\" encoding=\"utf-8\"?> \n <Person> \n");
            JSONObject jObj = JSONObject.parseObject(json);
            jsonToXmlstr(jObj,buffer);
            buffer.append("</Person>");
            return buffer.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }


    /**
     * Json to xmlstr string.
     *
     * @param jObj   the j obj
     * @param buffer the buffer
     * @return the string
     */
    public static String jsonToXmlstr(JSONObject jObj, StringBuffer buffer ){
        Set<Map.Entry<String, Object>> se = jObj.entrySet();
        for(Iterator<Map.Entry<String, Object>> it = se.iterator(); it.hasNext(); )
        {
            Map.Entry<String, Object> en = it.next();
            String key = en.getKey().substring(0,1).toUpperCase() + en.getKey().substring(1);
            if(en.getValue().getClass().getName().equals("net.sf.json.JSONObject")){
                buffer.append("<"+key+">");
                JSONObject jo = jObj.getJSONObject(en.getKey());
                jsonToXmlstr(jo,buffer);
                buffer.append("</"+key+"> \n");
            }else if(en.getValue().getClass().getName().equals("net.sf.json.JSONArray")){
                JSONArray jarray = jObj.getJSONArray(en.getKey());
                for (int i = 0; i < jarray.size(); i++) {
                    if(i==0) {
                        buffer.append("<" + key + ">");
                    }
                    buffer.append("<"+"Item"+">");
                    JSONObject jsonobject =  jarray.getJSONObject(i);
                    jsonToXmlstr(jsonobject,buffer);
                    buffer.append("</"+"Item"+">");
                    if(i<jarray.size()-1){
                        continue;
                    }
                    buffer.append("</"+key+"> \n");
                }
            }else if(en.getValue().getClass().getName().equals("java.lang.String")){
                buffer.append("<"+key+">"+en.getValue());
                buffer.append("</"+key+"> \n");
            }

        }
        return buffer.toString();
    }

    /**
     * lrm lrmx下载    --  下载到指定文件夹
     * @param dataInfoStr 数据信息字符串
     * @param fileName 文件名称
     * @return
     */
    public static void singleDataLRMOrLRMXExport(String dataInfoStr, String fileName){
        Writer out = null;
        try {
            File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
            String dataName = fileName + ".lrmx";
            String sourceFilePath = desktopDir.getAbsolutePath() + "/" + dataName;
            File file = new File(sourceFilePath);
            if (!file.exists()) {
                // 先得到文件的上级目录，并创建上级目录，在创建文件
                file.getParentFile().mkdir();
                file.createNewFile();
            }
            out = new PrintWriter(file, "UTF-8");
            out.write(dataInfoStr);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                if (out != null){
                    out.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }

}
